#!/usr/bin/env python
import codecs, logging, os
from datetime import datetime
from heblib import TanachParser

logging.basicConfig(level=logging.INFO, format='%(levelname)-8s %(message)s')
logger = logging.getLogger('com.metaist.hebphonics.build')

def main():  
  # Configuration
  config = {  
    '>tanach': os.path.join('corpus-tanach', '*.htm'),
    
    '<tmp': 'tmp',
    '<combined': os.path.join('tmp', 'combined.html'),
    '<scrubbed': os.path.join('tmp', 'scrubbed.html'),
    '<ignored':  os.path.join('tmp', 'ignored.log'),

    '<sql': os.path.join('db', 'words.sql'),
    '>db': 'db',
    '>db-build': os.path.join('.', 'build.sh')
  }
  
  # Check for tmp directory.
  if not os.path.isdir(config['<tmp']): 
    logger.info('Creating temporary directory [%s]...' % config['<tmp'])    
    os.mkdir(config['<tmp'])
    logger.debug('Temporary directory created.')

  sql, ignored = [], []
  
  # Build Tanach.
  tanach_sql, tanach_ignored = tanach_build(config)
  sql += tanach_sql
  ignored += tanach_ignored
  
  # Write SQL.
  with codecs.open(config['<sql'], encoding='utf_8', mode='w') as f:
    f.write('-- NOTE: This file is auto-generated from a script.\n')
    f.write('\n'.join(sql))
    version = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
    f.write('\nALTER TABLE hebphonics_words COMMENT = "Built %s";' % version)
  with codecs.open(config['<ignored'], encoding='utf_8', mode='w') as f: 
    f.write('\n'.join(ignored))
    
  # Run SQL.
  logger.info('Building database...')
  os.chdir(config['>db'])  
  os.system(config['>db-build'])
  os.chdir('..')  
  logger.debug('Database built.')
  
  # Done.
  logger.info('BUILD COMPLETtE.')

def tanach_build(config):
  '''Build the Tanach corpus.'''
  logger.info('Building Tanach corpus...')

  sql = [] # The result.
  ignored = []
  
  # Combine the corpus:
  logger.info('Combining Tanach corpus...')
  corpus_text = TanachParser.combine(config['>tanach'])
  with codecs.open(config['<combined'], encoding='utf_8', mode='w') as f: 
    f.write(corpus_text)
  logger.debug('Tanach corpus combined.')
  
  # Clean the corpus:
  logger.info('Cleaning Tanach corpus...')
  corpus_text = TanachParser.scrub(corpus_text)
  with codecs.open(config['<scrubbed'], encoding='utf_8', mode='w') as f: 
    f.write(corpus_text)
  logger.debug('Tanach corpus cleaned.')  
  
  # Tokenize the corpus:
  logger.info('Tokenizing Tanach corpus...')
  words = TanachParser.tokenize(corpus_text) 
  logger.debug('Tanach corpus tokenized.')

  logger.info('Generating SQL for Tanach corpus...')
  id = 0
  for (word, cols) in words.iteritems():
    if 0 == cols['syllable_count']:
      ignored.append('s:' + cols['hebrew'])
      continue # Do not include words without vowels.

    if cols['hashem_name']:
      ignored.append('h:' + cols['hebrew'])
      continue # Do not include names of HaShem.

    id += 1
    cols['id'] = id
    sql.append('INSERT INTO hebphonics_words VALUES '
      + unicode('({id}, _utf8"{hebrew}", "{english}", "{has_shva}", {syllable_count}, {frequency_count});').format(**cols))

  logger.debug('SQL generated for Tanach corpus.')
  
  return sql, ignored
 
if '__main__' == __name__: main()